package com.example.score.controllrt;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.example.score.common.AjaxResult;
import com.example.score.entity.*;
import com.example.score.entity.vo.TUserVo;
import com.example.score.entity.vo.TUserVos;
import com.example.score.service.ClassService;
import com.example.score.service.TeaclassuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
//import org.w3c.dom.css.CSSRule;

import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {
    @Autowired
    private ClassService classService;
    @Autowired
    private TeaclassuserService teaclassuserService;

    /**
     * 创建班级
     * @param teaclass
     * @return
     */
    @RequestMapping("/create")
    public AjaxResult create(Teaclass teaclass, Integer courseId,Integer userId) {
        if (teaclass == null || courseId < 0 || userId <=0) {
            return AjaxResult.fail(-1,"参数有误");
        }

        if (!StringUtils.hasLength(teaclass.getClassname()) || !StringUtils.hasLength(teaclass.getDescription())) {
            return AjaxResult.fail(-1,"参数有误");
        }
        Integer classId = classService.create(teaclass);
//        System.out.println(666);
//        System.out.println(classId);
        if (courseId > 0 && classId > 0){
            classService.joinCourse(classId,courseId,userId);
        }
        if (classId < 0) {
            return AjaxResult.fail(-2, "课程创建失败");
        }
        return AjaxResult.success(teaclass.getId());
    }

    /**
     * 根据Excel导入
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/importbyex")
    public AjaxResult importByExcel(MultipartFile file) throws IOException {
        if (file == null) {
            return AjaxResult.fail(-1, "参数错误");
        }
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<Teaclassuser> users = reader.readAll(Teaclassuser.class);
        System.out.println(6666);
        System.out.println(users);
        int inserts = teaclassuserService.insertbyex(users);
        if (inserts < 0) {
            return AjaxResult.fail(-2, "插入失败");
        }
        return AjaxResult.success("插入成功");
    }

    /**
     * 通过用户列表导入
     * @param list
     * @return
     */
    @RequestMapping("/importbylist")
    public AjaxResult importByList(Integer classId,  @RequestParam("list") List<Integer> list) {
        System.out.println(classId);
        System.out.println(list);
        if (list == null || list.size() <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        List<Teaclassuser> userlist = new ArrayList<>();
        for (Integer userId : list) {
            Teaclassuser teaclassuser = new Teaclassuser();
            teaclassuser.setClass_id(classId);
            teaclassuser.setUser_id(userId);
            userlist.add(teaclassuser);
        }
        // 先清除数据库
        teaclassuserService.clearByClassId(classId);
        // 插入数据
        System.out.println(userlist);
        int inserts = teaclassuserService.inserts(userlist);
        if (inserts < 0) {
            return AjaxResult.fail(-2, "插入失败");
        }
        return AjaxResult.success("插入成功");
    }

    /**
     * 删除班级成员
     * @param ids
     * @return
     */
    @RequestMapping("/delmembers")
    public AjaxResult deleteMembers(List<Integer> ids) {
        if (ids == null || ids.size() <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        int del = teaclassuserService.deleteMembers(ids);
        if (del >= 0) {
            return AjaxResult.success("删除成员成功");
        }
        return AjaxResult.fail(-2, "删除失败");
    }

    /**
     * 根据用户Id获取班级
     * @param userId
     * @return
     */
    @RequestMapping("/classlist")
    public AjaxResult getListById(Integer userId) {
        if (userId == null || userId <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        List<Teaclass> listById = classService.getListById(userId);
        if (listById != null && listById.size() > 0) {
            return AjaxResult.success(listById);
        }
        return AjaxResult.fail(-2, "获取班级列表失败");
    }

    /**
     * 获取班级成员
     * @param classId
     * @return
     */
    @RequestMapping("/members")
    public AjaxResult getMembersByClassId(Integer classId) {
        if (classId == null || classId <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        List<TUserVos> membersByClassId = teaclassuserService.getMembersByClassId(classId);
        if (membersByClassId != null && membersByClassId.size() >= 0) {
            return AjaxResult.success(membersByClassId);
        }
        return AjaxResult.fail(-2, "获取班级成员失败");
    }

    @RequestMapping("/AllClass")
    public AjaxResult showAllClassList(){
        List<Teaclass> AllList = teaclassuserService.showAllClassList();
        return AjaxResult.success(AllList);
    }

    @RequestMapping("/importGradeStudent")
    public AjaxResult importGradeStudent(Integer classId, Integer courseId){
        if (classId == null || classId <= 0 || courseId == null || courseId <= 0 ) {
            return AjaxResult.fail(-1, "参数错误");
        }
        teaclassuserService.clearGradeStudent(classId);
        teaclassuserService.importGradeStudent(classId,courseId);
        return AjaxResult.success("插入成功");
    }


    @RequestMapping("/RemainingClass")
    public AjaxResult RemainingClass(){
        List<Teaclass> AllList = teaclassuserService.RemainingClass();
        return AjaxResult.success(AllList);
    }

    @RequestMapping("/deleteClass")
    public AjaxResult deleteClass(Integer classId){
        if (classId == null || classId <= 0) {
            return AjaxResult.fail(-1, "参数错误");
        }
        int deleteClass = teaclassuserService.deleteClass(classId);
        return AjaxResult.success(deleteClass);
    }

    @RequestMapping("/selectMajors")
    public AjaxResult selectMajors(){
        List<major> majors = teaclassuserService.selectMajors();
        return AjaxResult.success(majors);
    }


    @RequestMapping("/addMajor")
    public AjaxResult addMajor(Integer major_id, String major_name){
        System.out.println(major_id);
        System.out.println(major_name);
        if (major_id == null || major_id <= 0 || major_name == null) {
            return AjaxResult.fail(-1, "参数错误");
        }
        int major = teaclassuserService.addMajor(major_id,major_name);
        if(major == 1){
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.fail(-2,"添加失败");
    }

    @PostMapping("/deleteMajor")
    public AjaxResult deleteMajor(@RequestBody List<Integer> lists) {
        System.out.println(777777);
        System.out.println(lists);
        if (lists == null || lists.isEmpty()) {
            return AjaxResult.fail(-1, "参数错误");
        }
        int major = teaclassuserService.delMajor(lists);
        if (major != 0) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.fail(-2, "删除失败");
    }


    @RequestMapping("/selectusers")
    public AjaxResult selectusers(String message){
        if (message == null) {
            return AjaxResult.fail(-1, "参数错误");
        }
        List<TUserVo> users = teaclassuserService.selectUsers(message);
        if(users != null){
            return AjaxResult.success(users);
        }
        return AjaxResult.fail(-2,"查询失败");
    }
}
